<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTool>
        <template slot="right">
          <el-button type="primary" @click="addPermission(1,'0')">新增权限</el-button>
        </template>
      </PageTool>
      <el-card>
        <el-table border :data="permissionList" :default-expand-all="true" row-key="id">
          <el-table-column label="权限名称" prop="name" width="180px" />
          <el-table-column label="权限标识" prop="code" />
          <el-table-column label="权限描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="{row}">
              <el-button v-if="row.type === 1" type="text" @click="addPermission(2,row.id)">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :title="dialogTittle" :visible="showDialog" :destroy-on-close="true" @close="cancelOperation">
      <el-form ref="form" label-width="80px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="激活状态">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
            active-color="#5889fe"
            inactive-color="#e4e4e4"
          />
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="confirmOperation">确认</el-button>
        <el-button @click="cancelOperation">取消</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, getPermissionDetail, updatePermission, delPermission } from '@/api/permission'
import { convertTreeData } from '@/utils'
export default {
  data() {
    return {
      showDialog: false,
      permissionList: [],
      formData: {
        enVisible: '1',
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: '0'
      },
      rules: {
        'name': [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        'code': [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    dialogTittle() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      try {
        const data = await getPermissionList()
        this.permissionList = convertTreeData(data, '0')
      } catch (error) {
        console.dir(error)
      }
    },
    addPermission(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    async confirmOperation() {
      try {
        const isValid = await this.$refs.form.validate()
        if (isValid) {
          if (this.formData.id) {
            await updatePermission(this.formData)
          } else {
            await addPermission(this.formData)
          }
          this.showDialog = false
          this.$message.success(this.formData.id ? '编辑成功' : '新增成功')
          this.getPermissionList()
        }
      } catch (error) {
        console.dir(error)
      }
    },
    async editPermission(id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
    async delPermission(id) {
      try {
        await this.$confirm('确认删除该权限?', '提示')
        await delPermission(id)
        this.$message.success('权限删除成功!')
        this.getPermissionList()
      } catch (error) {
        console.dir(error)
      }
    },
    cancelOperation() {
      this.formData = {
        enVisible: '1',
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: '0'
      }
      this.$refs.form.clearValidate()
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
